define(['jquery', 'bootstrap', 'backend', 'form', 'table', 'tinymce'], function ($, undefined, Backend, Form, Table, tinymce) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'general/article/index',
                    add_url: 'general/article/add',
                    edit_url: 'general/article/edit',
                    del_url: 'general/article/del',
                    multi_url: 'general/article/multi',
                    table: 'article'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        { field: 'state', checkbox: true },
                        { field: 'id', title: "ID" },
                        { field: 'category', title: "文章分类", searchable: false },
                        { field: 'category_id', title: "文章分类", visible: false, searchList: Config.categoryList },
                        { field: 'title', title: "标题" },
                        { field: 'summary', title: "摘要" },
                        { field: 'weigh', title: "权重", searchable: false },
                        { field: 'cover', title: "封面图", searchable: false , formatter: Controller.api.formatter.thumb, search: false },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            width: 150
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });

            //给添加按钮添加`data-area`属性
            $(".btn-add").data("area", ["98%", "100%"]);

            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone,.btn-edit").data("area", ["98%", "100%"]);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    var html = '';
                    if (value.indexOf("jpg") > -1 || value.indexOf("jpeg") > -1 || value.indexOf("png") > -1 || value.indexOf("gif") > -1) {
                        html = '<a href="' + row.cover + '" target="_blank"><img src="' + row.cover + '" alt="" style="max-height:60px;max-width:120px"></a>';
                    }
                    return '<div style="width:120px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + html + '</div>';
                },
            }
        }
    };
    return Controller;
});
